<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" label-width="68px">
      <el-form-item label="标题">
        <el-input
          v-model="queryParams.businessTitle"
          placeholder="请输入标题"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="内容类型" prop="businessType">
        <el-select v-model="queryParams.businessType" placeholder="内容类型" clearable size="small">
          <el-option
            v-for="dict in businesTypeOptions"
            :key="dict.dictValue"
            :label="dict.dictLabel"
            :value="dict.dictValue"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="状态" prop="status">
        <el-select v-model="queryParams.status" placeholder="状态" clearable size="small">
          <el-option
            v-for="dict in statusOptions"
            :key="dict.dictValue"
            :label="dict.dictLabel"
            :value="dict.dictValue"
          />
        </el-select>

      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button type="primary" icon="el-icon-search" size="mini"   v-hasPermi="['system:business:add']" @click="handleAdd">新增</el-button>
      </el-form-item>
    </el-form>
    <el-table v-loading="loading" :data="businessList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="标题" align="center" prop="businessTitle" />
      <el-table-column label="内容类型" align="center" prop="businessType" :formatter="businessFormat"/>
      <!--<el-table-column label="内容" align="center" prop="businessContent" />-->
      <!--<el-table-column label="报名人数" align="center" prop="person" />-->
      <!--<el-table-column label="置顶状态" align="center" prop="topping"  :formatter="toppingFormat" />-->
      <el-table-column label="状态" align="center" prop="status" :formatter="statusFormat"/>
      <el-table-column label="创建时间" align="center" prop="createTime">
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['system:business:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['system:business:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改创业内容对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="780px">
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="内容类型" prop="businessType">
          <el-select v-model="form.businessType" placeholder="请选择">
            <el-option
              v-for="dict in businesTypeOptions"
              :key="dict.dictValue"
              :label="dict.dictLabel"
              :value="dict.dictValue"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="标题" prop="businessTitle" v-show="form.businessType!=1">
          <el-input v-model="form.businessTitle" placeholder="请输入标题" />
        </el-form-item>
        <el-form-item label="状态">
          <el-radio-group v-model="form.status">
            <el-radio
              v-for="dict in statusOptions"
              :key="dict.dictValue"
              :label="dict.dictValue"
            >{{dict.dictLabel}}</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="报名人数" prop="person"  v-show="form.businessType==2">
          <el-input v-model="form.person" placeholder="请输入类型报名人数" />
        </el-form-item>
        <el-form-item label="置顶状态" prop="topping" v-show="form.businessType==3">
          <el-radio-group v-model="form.topping">
            <el-radio
              v-for="dict in toppingOptinos"
              :key="dict.dictValue"
              :label="dict.dictValue"
            >{{dict.dictLabel}}</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="颜色" prop="color" v-show="form.businessType==2">
          <el-input v-model="form.color" placeholder="请输入颜色" />
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" placeholder="请输入备注" />
        </el-form-item>
        <el-form-item label="文件" prop="file"  v-show="form.businessType!=3">
          <el-upload
            class="upload-demo"
            accept="image/jpeg,image/png"
            :action="url"
            :headers="headers"
            :on-remove="handleRemove"
            :on-success="handleImageSuccess"
            multiple
            list-type="picture"
            :limit="1"
            :on-exceed="handleExceed"
            :file-list="fileList">
            <el-button size="small" type="primary">点击上传</el-button>
          </el-upload>
        </el-form-item>
        <el-form-item label="视频"   v-show="form.businessType==3">
          <el-upload
            class="upload-demo"
            :action="url"
            :on-remove="handleRemove"
            :before-upload="beforeUploadVideo"
            :on-progress="uploadVideoProcess"
            :on-success="handleImageSuccess"
            multiple
            :limit="1"
            :on-exceed="handleExceed"
            :show-file-list="false"
            :file-list="fileList">
            <video v-show="!!form.file"
                   v-bind:src="form.file"
                   class="avatar video-avatar"
                   controls="controls" style="width: 640px;height: 368px">
              您的浏览器不支持视频播放
            </video>
            <el-button size="small"  type="primary">上传视频</el-button>
            <el-progress v-if="videoFlag == true"
                         type="circle"
                         v-bind:percentage="videoUploadPercent"
                         style="margin-top:7px;"></el-progress>
          </el-upload>

        </el-form-item>
        <el-form-item label="内容" prop="businessContent" v-show="form.businessType!=3">
          <Editor v-model="form.businessContent"/>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer" style="padding-top:20px">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listBusiness, getBusiness, delBusiness, addBusiness, updateBusiness, exportBusiness } from "@/api/system/business";
import Editor from '@/components/Editor';
import { getToken } from '@/utils/auth'
export default {
  name: "Business",
  components: {
    Editor
  },
  data() {
    return {
      fileList:[],
      // //上传headers
      headers:{
        'Authorization': null
      },
      //url  上传接口地址
      url:null,
      videoFlag: false,
      //是否显示进度条
      videoUploadPercent: "",
      //进度条的进度，
      isShowUploadVideo: false,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 总条数
      total: 0,
      // 创业内容表格数据
      businessList: [],
      statusOptions: [],
      // 内容类型数据字典
      businesTypeOptions: [],
      toppingOptinos:[],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        businessTitle: undefined,
        person: undefined,
        topping: undefined,
        businessType: undefined,
        color: undefined,
        file: undefined,
        businessContent: undefined,
        status: undefined,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      }
    };
  },
  created() {
    this.getList();
    this.getUrl()
    this.getDicts("sys_normal_disable").then(response => {
      this.statusOptions = response.data;
    });
    this.getDicts("sys_business_type").then(response => {
      this.businesTypeOptions = response.data;
    });
    this.getDicts("sys_topping").then(response => {
      this.toppingOptinos = response.data;
    });
  },
  methods: {
    /** 查询创业内容列表 */
    getList() {
      this.loading = true;
      listBusiness(this.queryParams).then(response => {
        this.businessList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    getUrl(){
      //******************************************************************************
      this.url =   process.env.VUE_APP_BASE_API+'/common/createOSS'
      this.headers.Authorization ='Bearer ' + getToken() // 让每个请求携带自定义token 请根据实际情况自行修改
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    beforeUploadVideo(file) {
      var fileSize = file.size / 1024 / 1024 < 100;
      if (!fileSize) {
        this.msgError("视频大小不能超过100MB");
        return false;
      }
      this.isShowUploadVideo = false;
    },
    //进度条
    uploadVideoProcess(event, file, fileList) {
      this.videoFlag = true;
      this.videoUploadPercent = file.percentage.toFixed(0) * 1;
    },
    businessFormat(row, column) {
      return this.selectDictLabel(this.businesTypeOptions, row.businessType);
    },
    toppingFormat(row, column) {
      return this.selectDictLabel(this.toppingOptinos, row.topping);
    },
    statusFormat(row, column) {
      return this.selectDictLabel(this.statusOptions, row.status);
    },
    // 表单重置
    reset() {
      this.form = {
        businessId: undefined,
        businessTitle: undefined,
        person: undefined,
        topping: "0",
        businessType: "1",
        color: undefined,
        remark: undefined,
        file: undefined,
        businessContent: undefined,
        status: "0",
        createBy: undefined,
        createTime: undefined,
        updateBy: undefined,
        updateTime: undefined
      };
      this.fileList=[];
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.businessId)
      this.single = selection.length!=1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加创业内容";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const businessId = row.businessId || this.ids
      getBusiness(businessId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改创业内容";
        if(!!row.file){
          var url = row.file
          var list =[]
          list.url  = url
          this.fileList.push( list)
        }
      });
    },
    /** 提交按钮 */
    submitForm: function() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.businessId != undefined) {
            updateBusiness(this.form).then(response => {
              if (response.code === 200) {
                this.msgSuccess("修改成功");
                this.open = false;
                this.getList();
              } else {
                this.msgError(response.msg);
              }
            });
          } else {
            addBusiness(this.form).then(response => {
              if (response.code === 200) {
                this.msgSuccess("新增成功");
                this.open = false;
                this.getList();
              } else {
                this.msgError(response.msg);
              }
            });
          }
        }
      });
    },

    handleExceed(files, fileList) {
      this.$message.warning(`当前限制选择 1 个文件，本次选择了 ${files.length} 个文件，共选择了 ${files.length + fileList.length} 个文件`);
    },
    handleImageSuccess(res, file, fileList) {
      this.isShowUploadVideo = true;
      this.videoFlag = false;
      this.videoUploadPercent = 0;
      this.form.file = fileList[0].response.url
    },
    handleRemove() {
      this.fileList.length=0
      this.form.file = ""
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const businessIds = row.businessId || this.ids;
      this.$confirm('是否确认删除创业内容编号为"' + businessIds + '"的数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return delBusiness(businessIds);
        }).then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        }).catch(function() {});
    },
  }
};
</script>
